import React from "react";
import ProgressView from "../../../view/ProgressView";
import styles from "../../../css/attendance.module.css";
import DateWeek from '../dateComponents/dateWeek'
import Punch from './punchComponent'
import { getDayRecord } from '../../../service/AttendService'
import { getTOKEN } from "../../../utils/StorageUtil";
class PunchScreen extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      selectedIndex: 0,
      selectDate: { record: null },
      loading: false
    };
  }
  componentWillMount() {
    // getClassNumInfo({
    //   token:getTOKEN()
    // })
  }

  setLoading = (loading) => {
    this.setState({
      loading: loading
    })
  }
  selectDate = (e) => {
    this.getDayRecord({
      date: e.year + '-' + e.month + '-' + e.showDate
    }, e)
    this.setState({
      selectDate: e
    })
  }
  // 获取当日考勤信息
  getDayRecord = (params, selectDate) => {
    this.setState({
      loading: true
    })
    let record = null
    let des = null
    let isPreCard = false
    const date = new Date()
    if(date.getFullYear() === selectDate.year&&
      date.getMonth() + 1 === selectDate.month&&
      date.getDate() === selectDate.showDate
  ){
    isPreCard = true
  }
    console.log(selectDate)    
    getDayRecord({
      token: getTOKEN(),
      ...params
    })
      .then(
        res => {
          this.setState({
            loading: false
          })
        
          if (res.error_code === 0) {
            record = res.data
          } else if (res.error_code === 300) {
            des = '今天休息哦'
          } else if (res.error_code === 316) {
            des = '日子还没到哦'
          } else if (res.error_code === 317) {
            des = '暂无记录'
          } else if (res.error_code === 315) {
            des = null
          } else {
            record = null
          }
          this.setState({
            selectDate: { 
              ...selectDate,
              record: record,
              isPreCard:isPreCard,
              default_des:des
            }
          })
        }
      )
      .catch(
        err => {
          console.log(err)
          this.setState({
            selectDate: { 
              ...selectDate,
              record: record,
              isPreCard:isPreCard,
              default_des:'获取信息失败'
            }
          })
        }
      )
  }
  navigate = (e) => {
    this.setState({
      selectedIndex: e
    })
  }
  // 打卡刷新
  reload = () => {
    const { selectDate } = this.state
    this.getDayRecord({
      date: selectDate.year + '-' + selectDate.month + '-' + selectDate.showDate,
    }, selectDate)
  }
  render() {
    return (
      <div>
        <div className={styles['attend_container']}>
          <DateWeek selectDate={this.selectDate} />
        </div>
        <div className={styles['attend_container']}
          onClick={this.reload}
        >
          <Punch selectDate={this.state.selectDate} reload={this.reload} setLoading={this.setLoading} />
        </div>
        <ProgressView visibility={this.state.loading} mode={ProgressView.MODE_MEDIUM} />

      </div>
    );
  }
}
export default PunchScreen;
